class Sound {
  constructor(file, audioCtx) {
    this.file = file
    this.audioCtx = audioCtx
  }
  createdSource() {
    const promise = new Promise((resolve, reject) => {
      this.audioCtx.decodeAudioData(this.file).then((buf) => {
        resolve(buf)
      }).catch((e) => {
        reject(e)
      })
    })
    return promise
  }
  playSource(audio) {
    if (this.audioCtx.state === 'suspended') {
      this.audioCtx.resume()
    }
    const source = this.audioCtx.createBufferSource()
    const gainNode = this.audioCtx.createGain()
    source.buffer = audio
    source.connect(gainNode)
    gainNode.connect(this.audioCtx.destination)
    return source
    // const source = this.audioCtx.createMediaElementSource(audio)
    // const gainNode = this.audioCtx.createGain()
    // source.connect(gainNode)
    // gainNode.connect(this.audioCtx.destination)
    // this.audioCtx.resume()
    // return source
  }
}

export default Sound
